#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
   @Author: 叶建伟
   @Email: yejw46507@hundsun.com
   @Time: 2022/6/14 15:20
   @File: 可疑特征配置.py
"""
import time

from qturboframe.lib.utils.by import By

from BladeFerm.Lib.Common import data_processing, logger
from 线下客户管理.线下客户共用脚本.open_menu import open_menu, arm_form_fill
from 基础公共脚本.步骤检查_hui2 import check_table, check_table_rows, FE, check_ele_text
from 基础公共脚本.common_util import check_tabs,click_next,click_submit


@data_processing
def suspicious_features_config(input_data=None, ui_expect=None):
    open_menu({'子系统名称': '反洗钱系统', '菜单名称': '可疑交易管理/可疑交易配置'})

    # 点击打开 等级区间方案
    time.sleep(1)
    list_obj = FE.list(idname={By.XPATH: '//div[@class="h-tabs-nav-right"]'})
    list_obj.foud_target(idname={By.XPATH: './div[contains(@class,"h-tabs-tab")]'})
    list_obj.click('可疑特征配置')

    # 点击 修改
    FE.button("修改").click()

    # 监测标准 tab页的信息更新
    if input_data.get("操作类型1", "新增") == "新增":
        FE.button("新增").click()
        fill_data(input_data.get("监测标准"))
    elif input_data.get("操作类型1", "新增") == "删除":
        FE.table_new().click_cell(input_data.get("定位数据1"), {"操作": "删除"})
    else:
        FE.table_new().click_cell(input_data.get("定位数据1"), {"操作": "修改"})
        fill_data(input_data.get("监测标准"))
    FE.button("下一步").click()

    # 可疑交易特征 tab页的信息更新
    if input_data.get("操作类型2", "新增") == "新增":
        FE.button("新增").click()
        fill_data(input_data.get("可疑交易特征"))
    elif input_data.get("操作类型2", "新增") == "删除":
        FE.table_new().click_cell(input_data.get("定位数据2"), {"操作": "删除"})
    else:
        FE.table_new().click_cell(input_data.get("定位数据2"), {"操作": "修改"})
        fill_data(input_data.get("可疑交易特征"))
    FE.button("下一步").click()

    # 提交
    FE.button(idname={'xpath': '//div[contains(@class,"submit-btn")]/button', 'text': '提交'}).click()
    check_ele_text(ui_expect=ui_expect)



def fill_data(input_data, option="保存"):
    __input_element = "h-input-wrapper"
    __select_element = "h-select-multiple"
    xp = '//div[@class="operator"]/a[text()="保存"]//ancestor::tr'
    table = FE.list(idname={'xpath': xp})
    for key, value in input_data.items():
        index, pos = key.split('|')
        class_list = table.get_target_class({'xpath': f'./td[{index}]/div/div'})
        if __input_element in class_list:
            input_ele1 = FE.input_type(idname={'xpath': f'./td[{index}]/div/div'}, father=table.list_type)
            input_ele1.set_value(input_data.get(key))
            logger.info(f'{pos} 设置值为 {value}')
        elif __select_element in class_list:
            select_ele1 = FE.select(idname={'xpath': f'./td[{index}]/div/div'}, father=table.list_type)
            select_ele1.set_value_for_multiple(value=input_data.get(key).split(','))
            select_ele1.click()
            logger.info(f'{pos} 选择值为 {value}')
            time.sleep(0.5)
        else:
            logger.error(f'{class_list} 中存在不支持的类型。')
    FE.list({'xpath': f'//div[@class="operator"]/a[text()="{option}"]'}).list_type.click()
